<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>node.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_editor_node.js.html">node.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utilties for working with DOM nodes related to rich text
<a name="line17"></a> * editing.  Many of these are not general enough to go into goog.dom.
<a name="line18"></a> *
<a name="line19"></a> * @author nicksantos@google.com (Nick Santos)
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.editor.node&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.iter.ChildIterator&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom.iter.SiblingIterator&#39;);
<a name="line29"></a>goog.require(&#39;goog.iter&#39;);
<a name="line30"></a>goog.require(&#39;goog.object&#39;);
<a name="line31"></a>goog.require(&#39;goog.string&#39;);
<a name="line32"></a>goog.require(&#39;goog.string.Unicode&#39;);
<a name="line33"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Names of all block-level tags
<a name="line38"></a> * @type {Object}
<a name="line39"></a> * @private
<a name="line40"></a> */
<a name="line41"></a>goog.editor.node.BLOCK_TAG_NAMES_ = goog.object.createSet(
<a name="line42"></a>    goog.dom.TagName.ADDRESS,
<a name="line43"></a>    goog.dom.TagName.ARTICLE,
<a name="line44"></a>    goog.dom.TagName.ASIDE,
<a name="line45"></a>    goog.dom.TagName.BLOCKQUOTE,
<a name="line46"></a>    goog.dom.TagName.BODY,
<a name="line47"></a>    goog.dom.TagName.CAPTION,
<a name="line48"></a>    goog.dom.TagName.CENTER,
<a name="line49"></a>    goog.dom.TagName.COL,
<a name="line50"></a>    goog.dom.TagName.COLGROUP,
<a name="line51"></a>    goog.dom.TagName.DETAILS,
<a name="line52"></a>    goog.dom.TagName.DIR,
<a name="line53"></a>    goog.dom.TagName.DIV,
<a name="line54"></a>    goog.dom.TagName.DL,
<a name="line55"></a>    goog.dom.TagName.DD,
<a name="line56"></a>    goog.dom.TagName.DT,
<a name="line57"></a>    goog.dom.TagName.FIELDSET,
<a name="line58"></a>    goog.dom.TagName.FIGCAPTION,
<a name="line59"></a>    goog.dom.TagName.FIGURE,
<a name="line60"></a>    goog.dom.TagName.FOOTER,
<a name="line61"></a>    goog.dom.TagName.FORM,
<a name="line62"></a>    goog.dom.TagName.H1,
<a name="line63"></a>    goog.dom.TagName.H2,
<a name="line64"></a>    goog.dom.TagName.H3,
<a name="line65"></a>    goog.dom.TagName.H4,
<a name="line66"></a>    goog.dom.TagName.H5,
<a name="line67"></a>    goog.dom.TagName.H6,
<a name="line68"></a>    goog.dom.TagName.HEADER,
<a name="line69"></a>    goog.dom.TagName.HGROUP,
<a name="line70"></a>    goog.dom.TagName.HR,
<a name="line71"></a>    goog.dom.TagName.ISINDEX,
<a name="line72"></a>    goog.dom.TagName.OL,
<a name="line73"></a>    goog.dom.TagName.LI,
<a name="line74"></a>    goog.dom.TagName.MAP,
<a name="line75"></a>    goog.dom.TagName.MENU,
<a name="line76"></a>    goog.dom.TagName.NAV,
<a name="line77"></a>    goog.dom.TagName.OPTGROUP,
<a name="line78"></a>    goog.dom.TagName.OPTION,
<a name="line79"></a>    goog.dom.TagName.P,
<a name="line80"></a>    goog.dom.TagName.PRE,
<a name="line81"></a>    goog.dom.TagName.SECTION,
<a name="line82"></a>    goog.dom.TagName.SUMMARY,
<a name="line83"></a>    goog.dom.TagName.TABLE,
<a name="line84"></a>    goog.dom.TagName.TBODY,
<a name="line85"></a>    goog.dom.TagName.TD,
<a name="line86"></a>    goog.dom.TagName.TFOOT,
<a name="line87"></a>    goog.dom.TagName.TH,
<a name="line88"></a>    goog.dom.TagName.THEAD,
<a name="line89"></a>    goog.dom.TagName.TR,
<a name="line90"></a>    goog.dom.TagName.UL);
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * Names of tags that have intrinsic content.
<a name="line95"></a> * TODO(robbyw): What about object, br, input, textarea, button, isindex,
<a name="line96"></a> * hr, keygen, select, table, tr, td?
<a name="line97"></a> * @type {Object}
<a name="line98"></a> * @private
<a name="line99"></a> */
<a name="line100"></a>goog.editor.node.NON_EMPTY_TAGS_ = goog.object.createSet(
<a name="line101"></a>    goog.dom.TagName.IMG, goog.dom.TagName.IFRAME, goog.dom.TagName.EMBED);
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * Check if the node is in a standards mode document.
<a name="line106"></a> * @param {Node} node The node to test.
<a name="line107"></a> * @return {boolean} Whether the node is in a standards mode document.
<a name="line108"></a> */
<a name="line109"></a>goog.editor.node.isStandardsMode = function(node) {
<a name="line110"></a>  return goog.dom.getDomHelper(node).isCss1CompatMode();
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Get the right-most non-ignorable leaf node of the given node.
<a name="line116"></a> * @param {Node} parent The parent ndoe.
<a name="line117"></a> * @return {Node} The right-most non-ignorable leaf node.
<a name="line118"></a> */
<a name="line119"></a>goog.editor.node.getRightMostLeaf = function(parent) {
<a name="line120"></a>  var temp;
<a name="line121"></a>  while (temp = goog.editor.node.getLastChild(parent)) {
<a name="line122"></a>    parent = temp;
<a name="line123"></a>  }
<a name="line124"></a>  return parent;
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Get the left-most non-ignorable leaf node of the given node.
<a name="line130"></a> * @param {Node} parent The parent ndoe.
<a name="line131"></a> * @return {Node} The left-most non-ignorable leaf node.
<a name="line132"></a> */
<a name="line133"></a>goog.editor.node.getLeftMostLeaf = function(parent) {
<a name="line134"></a>  var temp;
<a name="line135"></a>  while (temp = goog.editor.node.getFirstChild(parent)) {
<a name="line136"></a>    parent = temp;
<a name="line137"></a>  }
<a name="line138"></a>  return parent;
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Version of firstChild that skips nodes that are entirely
<a name="line144"></a> * whitespace and comments.
<a name="line145"></a> * @param {Node} parent The reference node.
<a name="line146"></a> * @return {Node} The first child of sibling that is important according to
<a name="line147"></a> *     goog.editor.node.isImportant, or null if no such node exists.
<a name="line148"></a> */
<a name="line149"></a>goog.editor.node.getFirstChild = function(parent) {
<a name="line150"></a>  return goog.editor.node.getChildHelper_(parent, false);
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Version of lastChild that skips nodes that are entirely whitespace or
<a name="line156"></a> * comments.  (Normally lastChild is a property of all DOM nodes that gives the
<a name="line157"></a> * last of the nodes contained directly in the reference node.)
<a name="line158"></a> * @param {Node} parent The reference node.
<a name="line159"></a> * @return {Node} The last child of sibling that is important according to
<a name="line160"></a> *     goog.editor.node.isImportant, or null if no such node exists.
<a name="line161"></a> */
<a name="line162"></a>goog.editor.node.getLastChild = function(parent) {
<a name="line163"></a>  return goog.editor.node.getChildHelper_(parent, true);
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Version of previoussibling that skips nodes that are entirely
<a name="line169"></a> * whitespace or comments.  (Normally previousSibling is a property
<a name="line170"></a> * of all DOM nodes that gives the sibling node, the node that is
<a name="line171"></a> * a child of the same parent, that occurs immediately before the
<a name="line172"></a> * reference node.)
<a name="line173"></a> * @param {Node} sibling The reference node.
<a name="line174"></a> * @return {Node} The closest previous sibling to sibling that is
<a name="line175"></a> *     important according to goog.editor.node.isImportant, or null if no such
<a name="line176"></a> *     node exists.
<a name="line177"></a> */
<a name="line178"></a>goog.editor.node.getPreviousSibling = function(sibling) {
<a name="line179"></a>  return /** @type {Node} */ (goog.editor.node.getFirstValue_(
<a name="line180"></a>      goog.iter.filter(new goog.dom.iter.SiblingIterator(sibling, false, true),
<a name="line181"></a>      goog.editor.node.isImportant)));
<a name="line182"></a>};
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Version of nextSibling that skips nodes that are entirely whitespace or
<a name="line187"></a> * comments.
<a name="line188"></a> * @param {Node} sibling The reference node.
<a name="line189"></a> * @return {Node} The closest next sibling to sibling that is important
<a name="line190"></a> *     according to goog.editor.node.isImportant, or null if no
<a name="line191"></a> *     such node exists.
<a name="line192"></a> */
<a name="line193"></a>goog.editor.node.getNextSibling = function(sibling) {
<a name="line194"></a>  return /** @type {Node} */ (goog.editor.node.getFirstValue_(
<a name="line195"></a>      goog.iter.filter(new goog.dom.iter.SiblingIterator(sibling),
<a name="line196"></a>      goog.editor.node.isImportant)));
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Internal helper for lastChild/firstChild that skips nodes that are entirely
<a name="line202"></a> * whitespace or comments.
<a name="line203"></a> * @param {Node} parent The reference node.
<a name="line204"></a> * @param {boolean} isReversed Whether children should be traversed forward
<a name="line205"></a> *     or backward.
<a name="line206"></a> * @return {Node} The first/last child of sibling that is important according
<a name="line207"></a> *     to goog.editor.node.isImportant, or null if no such node exists.
<a name="line208"></a> * @private
<a name="line209"></a> */
<a name="line210"></a>goog.editor.node.getChildHelper_ = function(parent, isReversed) {
<a name="line211"></a>  return (!parent || parent.nodeType != goog.dom.NodeType.ELEMENT) ? null :
<a name="line212"></a>      /** @type {Node} */ (goog.editor.node.getFirstValue_(goog.iter.filter(
<a name="line213"></a>          new goog.dom.iter.ChildIterator(
<a name="line214"></a>              /** @type {Element} */ (parent), isReversed),
<a name="line215"></a>          goog.editor.node.isImportant)));
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/**
<a name="line220"></a> * Utility function that returns the first value from an iterator or null if
<a name="line221"></a> * the iterator is empty.
<a name="line222"></a> * @param {goog.iter.Iterator} iterator The iterator to get a value from.
<a name="line223"></a> * @return {*} The first value from the iterator.
<a name="line224"></a> * @private
<a name="line225"></a> */
<a name="line226"></a>goog.editor.node.getFirstValue_ = function(iterator) {
<a name="line227"></a>  /** @preserveTry */
<a name="line228"></a>  try {
<a name="line229"></a>    return iterator.next();
<a name="line230"></a>  } catch (e) {
<a name="line231"></a>    return null;
<a name="line232"></a>  }
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/**
<a name="line237"></a> * Determine if a node should be returned by the iterator functions.
<a name="line238"></a> * @param {Node} node An object implementing the DOM1 Node interface.
<a name="line239"></a> * @return {boolean} Whether the node is an element, or a text node that
<a name="line240"></a> *     is not all whitespace.
<a name="line241"></a> */
<a name="line242"></a>goog.editor.node.isImportant = function(node) {
<a name="line243"></a>  // Return true if the node is not either a TextNode or an ElementNode.
<a name="line244"></a>  return node.nodeType == goog.dom.NodeType.ELEMENT ||
<a name="line245"></a>         node.nodeType == goog.dom.NodeType.TEXT &amp;&amp;
<a name="line246"></a>         !goog.editor.node.isAllNonNbspWhiteSpace(node);
<a name="line247"></a>};
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>/**
<a name="line251"></a> * Determine whether a node&#39;s text content is entirely whitespace.
<a name="line252"></a> * @param {Node} textNode A node implementing the CharacterData interface (i.e.,
<a name="line253"></a> *     a Text, Comment, or CDATASection node.
<a name="line254"></a> * @return {boolean} Whether the text content of node is whitespace,
<a name="line255"></a> *     otherwise false.
<a name="line256"></a> */
<a name="line257"></a>goog.editor.node.isAllNonNbspWhiteSpace = function(textNode) {
<a name="line258"></a>  return goog.string.isBreakingWhitespace(textNode.nodeValue);
<a name="line259"></a>};
<a name="line260"></a>
<a name="line261"></a>
<a name="line262"></a>/**
<a name="line263"></a> * Returns true if the node contains only whitespace and is not and does not
<a name="line264"></a> * contain any images, iframes or embed tags.
<a name="line265"></a> * @param {Node} node The node to check.
<a name="line266"></a> * @param {boolean=} opt_prohibitSingleNbsp By default, this function treats a
<a name="line267"></a> *     single nbsp as empty.  Set this to true to treat this case as non-empty.
<a name="line268"></a> * @return {boolean} Whether the node contains only whitespace.
<a name="line269"></a> */
<a name="line270"></a>goog.editor.node.isEmpty = function(node, opt_prohibitSingleNbsp) {
<a name="line271"></a>  var nodeData = goog.dom.getRawTextContent(node);
<a name="line272"></a>
<a name="line273"></a>  if (node.getElementsByTagName) {
<a name="line274"></a>    for (var tag in goog.editor.node.NON_EMPTY_TAGS_) {
<a name="line275"></a>      if (node.tagName == tag || node.getElementsByTagName(tag).length &gt; 0) {
<a name="line276"></a>        return false;
<a name="line277"></a>      }
<a name="line278"></a>    }
<a name="line279"></a>  }
<a name="line280"></a>  return (!opt_prohibitSingleNbsp &amp;&amp; nodeData == goog.string.Unicode.NBSP) ||
<a name="line281"></a>      goog.string.isBreakingWhitespace(nodeData);
<a name="line282"></a>};
<a name="line283"></a>
<a name="line284"></a>
<a name="line285"></a>/**
<a name="line286"></a> * Returns the length of the text in node if it is a text node, or the number
<a name="line287"></a> * of children of the node, if it is an element. Useful for range-manipulation
<a name="line288"></a> * code where you need to know the offset for the right side of the node.
<a name="line289"></a> * @param {Node} node The node to get the length of.
<a name="line290"></a> * @return {number} The length of the node.
<a name="line291"></a> */
<a name="line292"></a>goog.editor.node.getLength = function(node) {
<a name="line293"></a>  return node.length || node.childNodes.length;
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * Search child nodes using a predicate function and return the first node that
<a name="line299"></a> * satisfies the condition.
<a name="line300"></a> * @param {Node} parent The parent node to search.
<a name="line301"></a> * @param {function(Node):boolean} hasProperty A function that takes a child
<a name="line302"></a> *    node as a parameter and returns true if it meets the criteria.
<a name="line303"></a> * @return {?number} The index of the node found, or null if no node is found.
<a name="line304"></a> */
<a name="line305"></a>goog.editor.node.findInChildren = function(parent, hasProperty) {
<a name="line306"></a>  for (var i = 0, len = parent.childNodes.length; i &lt; len; i++) {
<a name="line307"></a>    if (hasProperty(parent.childNodes[i])) {
<a name="line308"></a>      return i;
<a name="line309"></a>    }
<a name="line310"></a>  }
<a name="line311"></a>  return null;
<a name="line312"></a>};
<a name="line313"></a>
<a name="line314"></a>
<a name="line315"></a>/**
<a name="line316"></a> * Search ancestor nodes using a predicate function and returns the topmost
<a name="line317"></a> * ancestor in the chain of consecutive ancestors that satisfies the condition.
<a name="line318"></a> *
<a name="line319"></a> * @param {Node} node The node whose ancestors have to be searched.
<a name="line320"></a> * @param {function(Node): boolean} hasProperty A function that takes a parent
<a name="line321"></a> *     node as a parameter and returns true if it meets the criteria.
<a name="line322"></a> * @return {Node} The topmost ancestor or null if no ancestor satisfies the
<a name="line323"></a> *     predicate function.
<a name="line324"></a> */
<a name="line325"></a>goog.editor.node.findHighestMatchingAncestor = function(node, hasProperty) {
<a name="line326"></a>  var parent = node.parentNode;
<a name="line327"></a>  var ancestor = null;
<a name="line328"></a>  while (parent &amp;&amp; hasProperty(parent)) {
<a name="line329"></a>    ancestor = parent;
<a name="line330"></a>    parent = parent.parentNode;
<a name="line331"></a>  }
<a name="line332"></a>  return ancestor;
<a name="line333"></a>};
<a name="line334"></a>
<a name="line335"></a>
<a name="line336"></a>/**
<a name="line337"></a>* Checks if node is a block-level html element. The &lt;tt&gt;display&lt;/tt&gt; css
<a name="line338"></a> * property is ignored.
<a name="line339"></a> * @param {Node} node The node to test.
<a name="line340"></a> * @return {boolean} Whether the node is a block-level node.
<a name="line341"></a> */
<a name="line342"></a>goog.editor.node.isBlockTag = function(node) {
<a name="line343"></a>  return !!goog.editor.node.BLOCK_TAG_NAMES_[node.tagName];
<a name="line344"></a>};
<a name="line345"></a>
<a name="line346"></a>
<a name="line347"></a>/**
<a name="line348"></a> * Skips siblings of a node that are empty text nodes.
<a name="line349"></a> * @param {Node} node A node. May be null.
<a name="line350"></a> * @return {Node} The node or the first sibling of the node that is not an
<a name="line351"></a> *     empty text node. May be null.
<a name="line352"></a> */
<a name="line353"></a>goog.editor.node.skipEmptyTextNodes = function(node) {
<a name="line354"></a>  while (node &amp;&amp; node.nodeType == goog.dom.NodeType.TEXT &amp;&amp;
<a name="line355"></a>      !node.nodeValue) {
<a name="line356"></a>    node = node.nextSibling;
<a name="line357"></a>  }
<a name="line358"></a>  return node;
<a name="line359"></a>};
<a name="line360"></a>
<a name="line361"></a>
<a name="line362"></a>/**
<a name="line363"></a> * Checks if an element is a top-level editable container (meaning that
<a name="line364"></a> * it itself is not editable, but all its child nodes are editable).
<a name="line365"></a> * @param {Node} element The element to test.
<a name="line366"></a> * @return {boolean} Whether the element is a top-level editable container.
<a name="line367"></a> */
<a name="line368"></a>goog.editor.node.isEditableContainer = function(element) {
<a name="line369"></a>  return element.getAttribute &amp;&amp;
<a name="line370"></a>      element.getAttribute(&#39;g_editable&#39;) == &#39;true&#39;;
<a name="line371"></a>};
<a name="line372"></a>
<a name="line373"></a>
<a name="line374"></a>/**
<a name="line375"></a> * Checks if a node is inside an editable container.
<a name="line376"></a> * @param {Node} node The node to test.
<a name="line377"></a> * @return {boolean} Whether the node is in an editable container.
<a name="line378"></a> */
<a name="line379"></a>goog.editor.node.isEditable = function(node) {
<a name="line380"></a>  return !!goog.dom.getAncestor(node, goog.editor.node.isEditableContainer);
<a name="line381"></a>};
<a name="line382"></a>
<a name="line383"></a>
<a name="line384"></a>/**
<a name="line385"></a> * Finds the top-most DOM node inside an editable field that is an ancestor
<a name="line386"></a> * (or self) of a given DOM node and meets the specified criteria.
<a name="line387"></a> * @param {Node} node The DOM node where the search starts.
<a name="line388"></a> * @param {function(Node) : boolean} criteria A function that takes a DOM node
<a name="line389"></a> *     as a parameter and returns a boolean to indicate whether the node meets
<a name="line390"></a> *     the criteria or not.
<a name="line391"></a> * @return {Node} The DOM node if found, or null.
<a name="line392"></a> */
<a name="line393"></a>goog.editor.node.findTopMostEditableAncestor = function(node, criteria) {
<a name="line394"></a>  var targetNode = null;
<a name="line395"></a>  while (node &amp;&amp; !goog.editor.node.isEditableContainer(node)) {
<a name="line396"></a>    if (criteria(node)) {
<a name="line397"></a>      targetNode = node;
<a name="line398"></a>    }
<a name="line399"></a>    node = node.parentNode;
<a name="line400"></a>  }
<a name="line401"></a>  return targetNode;
<a name="line402"></a>};
<a name="line403"></a>
<a name="line404"></a>
<a name="line405"></a>/**
<a name="line406"></a> * Splits off a subtree.
<a name="line407"></a> * @param {!Node} currentNode The starting splitting point.
<a name="line408"></a> * @param {Node=} opt_secondHalf The initial leftmost leaf the new subtree.
<a name="line409"></a> *     If null, siblings after currentNode will be placed in the subtree, but
<a name="line410"></a> *     no additional node will be.
<a name="line411"></a> * @param {Node=} opt_root The top of the tree where splitting stops at.
<a name="line412"></a> * @return {!Node} The new subtree.
<a name="line413"></a> */
<a name="line414"></a>goog.editor.node.splitDomTreeAt = function(currentNode,
<a name="line415"></a>    opt_secondHalf, opt_root) {
<a name="line416"></a>  var parent;
<a name="line417"></a>  while (currentNode != opt_root &amp;&amp; (parent = currentNode.parentNode)) {
<a name="line418"></a>    opt_secondHalf = goog.editor.node.getSecondHalfOfNode_(parent, currentNode,
<a name="line419"></a>        opt_secondHalf);
<a name="line420"></a>    currentNode = parent;
<a name="line421"></a>  }
<a name="line422"></a>  return /** @type {!Node} */(opt_secondHalf);
<a name="line423"></a>};
<a name="line424"></a>
<a name="line425"></a>
<a name="line426"></a>/**
<a name="line427"></a> * Creates a clone of node, moving all children after startNode to it.
<a name="line428"></a> * When firstChild is not null or undefined, it is also appended to the clone
<a name="line429"></a> * as the first child.
<a name="line430"></a> * @param {!Node} node The node to clone.
<a name="line431"></a> * @param {!Node} startNode All siblings after this node will be moved to the
<a name="line432"></a> *     clone.
<a name="line433"></a> * @param {Node|undefined} firstChild The first child of the new cloned element.
<a name="line434"></a> * @return {!Node} The cloned node that now contains the children after
<a name="line435"></a> *     startNode.
<a name="line436"></a> * @private
<a name="line437"></a> */
<a name="line438"></a>goog.editor.node.getSecondHalfOfNode_ = function(node, startNode, firstChild) {
<a name="line439"></a>  var secondHalf = /** @type {!Node} */(node.cloneNode(false));
<a name="line440"></a>  while (startNode.nextSibling) {
<a name="line441"></a>    goog.dom.appendChild(secondHalf, startNode.nextSibling);
<a name="line442"></a>  }
<a name="line443"></a>  if (firstChild) {
<a name="line444"></a>    secondHalf.insertBefore(firstChild, secondHalf.firstChild);
<a name="line445"></a>  }
<a name="line446"></a>  return secondHalf;
<a name="line447"></a>};
<a name="line448"></a>
<a name="line449"></a>
<a name="line450"></a>/**
<a name="line451"></a> * Appends all of oldNode&#39;s children to newNode. This removes all children from
<a name="line452"></a> * oldNode and appends them to newNode. oldNode is left with no children.
<a name="line453"></a> * @param {!Node} newNode Node to transfer children to.
<a name="line454"></a> * @param {Node} oldNode Node to transfer children from.
<a name="line455"></a> * @deprecated Use goog.dom.append directly instead.
<a name="line456"></a> */
<a name="line457"></a>goog.editor.node.transferChildren = function(newNode, oldNode) {
<a name="line458"></a>  goog.dom.append(newNode, oldNode.childNodes);
<a name="line459"></a>};
<a name="line460"></a>
<a name="line461"></a>
<a name="line462"></a>/**
<a name="line463"></a> * Replaces the innerHTML of a node.
<a name="line464"></a> *
<a name="line465"></a> * IE has serious problems if you try to set innerHTML of an editable node with
<a name="line466"></a> * any selection. Early versions of IE tear up the old internal tree storage, to
<a name="line467"></a> * help avoid ref-counting loops. But this sometimes leaves the selection object
<a name="line468"></a> * in a bad state and leads to segfaults.
<a name="line469"></a> *
<a name="line470"></a> * Removing the nodes first prevents IE from tearing them up. This is not
<a name="line471"></a> * strictly necessary in nodes that do not have the selection. You should always
<a name="line472"></a> * use this function when setting innerHTML inside of a field.
<a name="line473"></a> *
<a name="line474"></a> * @param {Node} node A node.
<a name="line475"></a> * @param {string} html The innerHTML to set on the node.
<a name="line476"></a> */
<a name="line477"></a>goog.editor.node.replaceInnerHtml = function(node, html) {
<a name="line478"></a>  // Only do this IE. On gecko, we use element change events, and don&#39;t
<a name="line479"></a>  // want to trigger spurious events.
<a name="line480"></a>  if (goog.userAgent.IE) {
<a name="line481"></a>    goog.dom.removeChildren(node);
<a name="line482"></a>  }
<a name="line483"></a>  node.innerHTML = html;
<a name="line484"></a>};
</pre>


</body>
</html>
